package com.zlh.hdfsfilesys.controller;

import com.zlh.hdfsfilesys.entity.User;
import com.zlh.hdfsfilesys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 找回密码页面（fspwd.html）的控制器
 */
@Controller
public class FindpwdController {

    @Autowired
    UserMapper userMapper;
    //响应忘记密码按钮，进入忘记密码页面
    @RequestMapping(value = "/tofindpwd")
    public String tofindpwd(Model model){
        User user = new User(0,"","","");
        model.addAttribute(user);
        return "fdpwd";
    }


    /**
     * 响应带参找回页面密码请求。
     * 根据用户输入的姓名和电话号码，搜索数据库，找回密码并显示
     * @param name  用户输入的姓名
     * @param tel 用户输入的电话
     * @param model
     * @return 成功之后返回页面，并显示密码。失败后会返回注册亚眠
     */
    @RequestMapping(value = "/findpwd")
    public String findpwd(@RequestParam("name")String name,@RequestParam("tel")String tel, Model model){
        User user = new User(0,"","","");
        model.addAttribute(user);
        List<User> userList = userMapper.selFindPwd(name, tel);
        if (userList.size()==0){
            System.out.println("失败--------------------");
            return "failed";
        }else {
            System.out.println("成功----------------");
            model.addAttribute("pwd",userList.get(0).getPassword());
            return "getpwd";
        }
    }
}
